package TRIR.XMLParser;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class XmlParser {
	
	Document tusDoc;
	Document kolosejDoc;
	ArrayList<Predstava> vsePredstave= new ArrayList<Predstava>();
	

	public ArrayList<Predstava> getAllShows(){
		try {
			getKolosejDocument();
			getTusDocument();
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		return vsePredstave;
	}
	
	public ArrayList<Predstava> getAllShows(String city, String kino){
		getAllShows();
		ArrayList<Predstava> filtrirano=new ArrayList<Predstava>();
		for(Predstava p : vsePredstave){
			if(p.toString().contains(city) && (p.toString().contains(kino))){
				filtrirano.add(p);
			}
		}
		
		
		
		return filtrirano;
	}
	
	
	private void getTusDocument() throws ParserConfigurationException, MalformedURLException, SAXException, IOException{
		final String tusUrl="http://maribor.planet-tus.si/xml/spored.xml";
		DocumentBuilderFactory dbf= DocumentBuilderFactory.newInstance();
		DocumentBuilder builder = dbf.newDocumentBuilder();
		tusDoc=builder.parse(new URL(tusUrl).openStream());
		
		NodeList nList=tusDoc.getElementsByTagName("dan");
		for(int i=0; i<nList.getLength(); i++){
			Node n=nList.item(i);
			if(n.getNodeType()==Node.ELEMENT_NODE){
				Element e=(Element) n;
				String date=e.getAttribute("datum");
				//System.out.println(date);
				NodeList sporedList=e.getElementsByTagName("spored");
				for (int j = 0; j < sporedList.getLength(); j++) {
					Node kino=sporedList.item(j);
					Element eKino=(Element) kino;
					String center=eKino.getAttribute("kino");
					
					
					NodeList filmList=eKino.getElementsByTagName("film");
					for (int k = 0; k < filmList.getLength(); k++) {
						Node film=filmList.item(k);
						Element eFilm=(Element) film;
						String titleSlo=getTagValue("naslov_si", eFilm);
						String titleEng=getTagValue("naslov_en", eFilm);
						

						NodeList predstaveList=eFilm.getElementsByTagName("ura");
						for (int l = 0; l < predstaveList.getLength(); l++) {
							Node predstava=predstaveList.item(l);
							Element eUra=(Element)predstava;
							String theater = eUra.getAttribute("theatre");
							String time=predstava.getTextContent();
							String city="";
							if(center.toLowerCase().contains("koper")){
								city="Koper";
							}else if(center.toLowerCase().contains("maribor")){
								city="Maribor";
							}else if(center.toLowerCase().contains("ljubljana")){
								city="Ljubljana";
							}else if(center.toLowerCase().contains("celje")){
								city="Celje";
							}else if(center.toLowerCase().contains("novo")){
								city="Novo Mesto";
							}else if(center.toLowerCase().contains("kranj")){
								city="Kranj";
							}
							
							Predstava p=new Predstava(titleSlo, titleEng, time, date, city, theater, center);
							vsePredstave.add(p);
							
						}
					}
				}
			}
		}
		
		
		
	}
	private void getKolosejDocument() throws ParserConfigurationException, MalformedURLException, SAXException, IOException{
		final String kolosejUrl="http://www.kolosej.si/spored/xml/2.0/";
		DocumentBuilderFactory dbf= DocumentBuilderFactory.newInstance();
		DocumentBuilder builder = dbf.newDocumentBuilder();
		kolosejDoc= builder.parse(new URL(kolosejUrl).openStream());
		kolosejDoc.getDocumentElement().normalize();
		
		NodeList nList= kolosejDoc.getElementsByTagName("movie");
	
		
		for(int i=0; i<nList.getLength(); i++){
			//nodelist vseh movie tagov
			Node n= nList.item(i);
			if(n.getNodeType() ==Node.ELEMENT_NODE){
				Element e=(Element) n;
				String titleSlo=getTagValue("title", e);
				String titleEng=getTagValue("original_title", e);
				//System.out.println(titleEng);
				
				
				NodeList showList=e.getElementsByTagName("show");
				for(int j=0; j<showList.getLength(); j++){
					Node n1=showList.item(j);
					Element er = (Element) n1;
					
					String date=getTagValue("date", er);
					String time=getTagValue("time", er);
					String city=getTagValue("city", er);
					String center=getTagValue("center", er);
					String theater=getTagValue("theater", er);
					//System.out.println(date +"   catj  "+time+ "   kraj  : "+city);
					
					Predstava p= new Predstava(titleSlo, titleEng, time, date, city, theater, center);
					vsePredstave.add(p);
					
				}
			}
			
		}
	}
	
	  private String getTagValue(String sTag, Element eElement) {
			NodeList nlList = eElement.getElementsByTagName(sTag).item(0).getChildNodes();
		 
	        Node nValue = (Node) nlList.item(0);
		 
			return nValue.getNodeValue();
		  }
	
	
}
